package com.example.myapplication.Adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.myapplication.Data.Course;
import com.example.myapplication.R;

import java.util.List;

public class CourseAdapter extends ArrayAdapter<Course> {
    private List<Course> mCourse;
    private Context mContext;
    private int resourceId;

    public CourseAdapter(Context context,
                         int resourceId, List<Course> data) {
        super(context, resourceId, data);
        this.mContext = context;
        this.mCourse = data;
        this.resourceId = resourceId;
    }

    @Override
    public View getView(int position,
                        View convertView, ViewGroup parent) {
        Course Course = getItem(position);

        View view ;

        final ViewHolder vh;

        if (convertView == null) {
            view = LayoutInflater.from(getContext())
                    .inflate(resourceId, parent, false);

            vh = new ViewHolder();
            vh.tvCourseName = view.findViewById(R.id.tv_sign_in_course_name);
            vh.tvSource = view.findViewById(R.id.tv_subtitle);
            vh.ivCoursePhoto = view.findViewById(R.id.iv_course_photo);
            vh.tvCollegeName = view.findViewById(R.id.tv_college_name);

            view.setTag(vh);
        } else {
            view = convertView;
            vh = (ViewHolder) view.getTag();
        }
        Log.d("Total", String.valueOf(Course.getCourseName()));
        //显示文字
        vh.tvCourseName.setText(Course.getCourseName());
        vh.tvSource.setText(Course.getCourseId());
        vh.tvCollegeName.setText(Course.getCollegeName());
        //渲染图片
        Glide.with(mContext).load(Course.getCoursePhoto())
                .into(vh.ivCoursePhoto);

        return view;
    }

    class ViewHolder {
        TextView tvCourseName;
        TextView tvSource;
        ImageView ivCoursePhoto;
        TextView tvCollegeName;

    }
}
